﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using System;
using VOL.Entity.DomainModels;
using VOL.Core.Utilities;
using VOL.Team.IServices;
using VOL.Team.TeamIservices.MaterialManagement.IServices;
using Yitter.IdGenerator;

namespace VOL.WebApi.Controllers.Team.bpy
{
    /// <summary>
    /// BOM相关控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BomController : ControllerBase
    {
        /// <summary>
        /// bom服务接口
        /// </summary>
        private readonly IBomServices _bomService;

        public BomController(IBomServices bomService)
        {
            _bomService = bomService;
        }


        /// <summary>
        /// 新增BOM
        /// </summary>
        /// <param name="bOM">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<WebResponseContent> AddBomAsync([FromBody] BOM bOM)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            int i = await _bomService.AddBomAsync(bOM);
            return i > 0 ? webResponseContent.OK("BOM信息添加成功") : webResponseContent.Error("BOM信息添加失败");

        }
        
        /// <summary>
        /// 修改BOM
        /// </summary>
        /// <param name="bOM">要添加的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<WebResponseContent> UpdateBomAsync([FromBody] BOM bOM)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            int i = await _bomService.UpdateBomAsync(bOM);
            return i > 0 ? webResponseContent.OK("BOM信息修改成功") : webResponseContent.Error("BOM信息修改失败");

        }
        /// <summary>
        /// 获取物料包数据
        /// </summary>
        /// <returns>返回物料信息</returns>
        [HttpGet]
        public WebResponseContent GetMaterialPicking()
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var materialPicking = _bomService.GetMaterialPicking();
            return webResponseContent.OK("BOM信息添加成功", materialPicking);
        }
        /// <summary>
        /// 获取BOM数据
        /// </summary>
        /// <returns>返回BOM信息</returns>
        [HttpGet]
        public WebResponseContent GetBom(string? materialCode, string? bomCode)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            var bOM = _bomService.GetBom(materialCode,bomCode);
            return webResponseContent.OK("BOM信息查询成功", bOM);
        }

        /// <summary>
        /// 删除BOM
        /// </summary>
        /// <param name="bOM">要删除数据的id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<WebResponseContent> DeleteBomAsync(string id)
        {
            WebResponseContent webResponseContent = new WebResponseContent();
            int i = await _bomService.DeleteBomAsync(id);
            return i > 0 ? webResponseContent.OK("BOM信息删除成功") : webResponseContent.Error("BOM信息删除失败");
        }

        /// <summary>
        /// 获取一个雪花id用于生成bom编码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetBomCode()
        {
            var bomCode = YitIdHelper.NextId().ToString();
            return bomCode;
        }
    }
}
